package com.wnxy.admin.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wnxy.admin.entity.Coupon;
import com.wnxy.admin.mapper.CouponMapper;
import com.wnxy.admin.service.ICouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 优惠券信息表 服务实现类
 * </p>
 *
 * @author (^･ｪ･^)Cat~
 * @since 2023-12-04
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {

    private final CouponMapper couponMapper;
    public CouponServiceImpl(CouponMapper couponMapper){
        this.couponMapper = couponMapper;
    }

    @Override
    public Coupon chooseCoupon(Integer customerId) {
        Integer count = Math.toIntExact(countCoupon());
        Coupon coupon = couponMapper.selectOne(Wrappers.lambdaQuery(Coupon.class)
                .eq(Coupon::getCouponId, RandomUtil.randomInt(1, count)));
        return coupon;
    }

    @Override
    public void removeCouponByCustomerId(Integer customerId, Integer couponId) {
        // It has not decided yet, how to deal with ψ(._. )>
    }

    @Override
    public Long countCoupon() {
        return couponMapper.selectCount(Wrappers.lambdaQuery(Coupon.class));
    }


}
